package com.hanko.socket;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author: hanko
 * @Date: 2021-6-16 17:38
 */
@Slf4j
public class Server {

    @SneakyThrows
    public static void main(String[] args) {

        ServerSocket server = null;
        try {
            server = new ServerSocket(50000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true) {
            Socket client = null;
            DataInputStream input = null;
            DataOutputStream output = null;
            String data = null;
            try {
                client = server.accept();
                input = new DataInputStream(client.getInputStream());
                output = new DataOutputStream(client.getOutputStream());
                data = input.readUTF();
                log.info("接收数据：{}", data);
                Thread.sleep(5 * 1000);
                output.writeUTF("Server:  " + data);
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
